
******************************************************
* 				Replication do-file                  *
*         Review of Economics and Statistics         *
*                                                    *
*               Choosing Your Pond                   *
*       Location Choices and Relative Income         *
*                                                    *
*		Nicolas Bottan & Ricardo Perez-Truglia       *
*                                                    *
*                   September 2020                   *
*                                                    *
******************************************************
/*

                    Notes
					
This do-file generates Tables B1-B3 from the paper. 

Data from MTurk experiment.

*** Install Stata commands
cap: ssc install renames
cap: ssc install ivreg2
cap: ssc install ranktest 
cap: ssc install esttab
cap: ssc install outreg2
cap: ssc install parmest
cap: ssc install qqvalue
cap: ssc install quantiles
cap: ssc install outtable
*/



*** Set WD
cd ""		// Set path for main folder here


*** Appendix Table B1: Comparison of Characteristics between Subjects in Main and Auxiliary Samples
use "Mturk Experiment/mturk_experiment_clean.dta", clear

gen experiment=0

*append using "Main Experiment/mainexperiment_confidential.dta", force  // Confidential data
*replace experiment=1 if experiment==.

gen married=marital==2
gen haskids=nkids>0
drop if baseline_finalrank==.

*** Small program to add stars
cap: program drop stars
program define stars
args pval
if `pval'<=.01 {
	global s="***"
	}
else if `pval'>.01 & `pval'<=.05 {
	global s="**"
	}
else if `pval'>.05 & `pval'<=.1 {
	global s="*"
	}
else {
	global s=""
	}
end


postfile desc str20 variable str12 MainExperiment str12 AuxExperiment str12 Difference using results/AppendixTableB1.dta, replace

foreach var in age male married haskids {

	sum `var' if experiment==1
	local m1=string(r(mean), "%9.3f")
	local s1=string(r(sd)/sqrt(r(N)), "%9.3f")

	sum `var' if experiment==0
	local m2=string(r(mean), "%9.3f")
	local s2=string(r(sd)/sqrt(r(N)), "%9.3f")

	reg `var' experiment, r
	local b=string(_b[experiment], "%9.3f")
	local s3=string(_se[experiment], "%9.3f")
	test experiment
	local p=r(p)
	stars `p'
	local b = "`b'"+"$s"
	
	
	post desc ("`var'") ("`m1'") ("`m2'") ("`b'")
	post desc ("") ("(`s1')") ("(`s2')") ("(`s2')")
	
	}

cou if experiment==1
local m1=r(N)
cou if experiment==0
local m2=r(N)

post desc ("Observations") ("`m1'") ("`m2'") ("")

postclose desc










*** Appendix Table B2: Experimental Variation, Learning, and Perceptions in the Auxiliary Sample
use "Mturk Experiment/mturk_experiment_clean.dta", clear

global controls "knows2 rel_knows rel_qol lnpop reldens relblack reldemocrat relurban"

gen rel_inc=post_relinc
gen rel_px=post_relpx

forvalues i=1/2 {
local k=0
foreach var in inc px schools crime health pubspace environment entertain college trump {
local ++k
	preserve
	if `i'==1 {
	parmby "reg rel_`var' relinc_diff relpx_diff relinc_cfact relpx_cfact , r", label norestore
	}
	else {
	parmby "reg rel_`var' relinc_diff relpx_diff relinc_cfact relpx_cfact $controls , r", label norestore
	}
	drop if parm=="_cons"
	gen model="`k'"
	gen depvar="`var'"
	gen iter=`i'
	gen sample=e(N)
	if `k'==1 {
		tempfile aux
		}
	else {
		append using "`aux'"
		}
	save "`aux'", replace
	restore

	}
	
	preserve
	
	use "`aux'", clear
	qqvalue p, method(simes) qvalue(myqval)
	keep if parm=="relinc_diff" | parm=="relpx_diff"

	if `i'==1 {
		tempfile res
		save "`res'", replace
		}
	else {
		append using "`res'"
		save "`res'", replace
	}
	restore
	
	}

preserve
use "`res'", clear

destring model, replace
sort iter parm model 
drop parmseq label stderr dof t min95 max95 
order sample
	
keep if iter==2
keep parm depvar estimate p myqval
order parm depvar estimate

list
matrix define T=J(6,10,.)
forvalues i=1/10 {
	matrix T[1,`i']=round(estimate[`i'],.001)
	matrix T[2,`i']=round(p[`i'],.001)
	matrix T[3,`i']=round(myqval[`i'],.001)
}
forvalues i=11/20 {
	local j=`i'-10
	matrix T[4,`j']=round(estimate[`i'],.001)
	matrix T[5,`j']=round(p[`i'],.001)
	matrix T[6,`j']=round(myqval[`i'],.001)
}

matrix rownames T = "\DeltaER_{1,2}" "\emph{P-value}" "\emph{Q-value}" "\DeltaCOL_{1,2}" "\emph{P-value}" "\emph{Q-value}"
matrix colnames T = "ER^{post}_{1,2}" "COL^{post}_{1,2}" "Schools" "Health" "Spaces" "Environment" "Entertainment" "Crime" "CollegeGraduates" "VoteTrump"

outtable using "results/AppendixTableB2", mat(T) replace center nobox








*** Appendix Table B3: Location Preferences: Auxiliary Experiment

use "Mturk Experiment/mturk_experiment_clean.dta", clear

global controls "knows2 rel_knows rel_qol lnpop reldens relblack reldemocrat relurban"
global perceptions "rel_schools rel_crime rel_health rel_pubspace rel_environment rel_entertain rel_college rel_trump"

eststo clear
* ALL
cap: drop RELINC0 RELPX0
gen RELINC0=post_relinc
gen RELPX0=post_relpx

probit baseline_finalrank RELPX0 RELINC0  $controls , robust
eststo row1

ivprobit baseline_finalrank (RELPX0 RELINC0 = relinc_diff relpx_diff) relpx_cfact relinc_cfact $controls, vce(robust)
eststo row2

ivprobit baseline_finalrank (RELINC0 RELPX0 = relinc_diff relpx_diff) relpx_cfact relinc_cfact $controls $perceptions, vce(robust)
eststo row3

esttab, keep(RELINC0 RELPX0) order(RELINC0 RELPX0) se

mat list r(coefs)
mat define A=r(coefs)
eststo drop row*
local rnames : rownames A
local models : coleq A
local models : list uniq models

local i=0
foreach name in RELINC0 RELPX0 {
 local ++i
 local j 0
 cap: matrix drop b
 cap: matrix drop se
 cap: matrix drop p
 foreach model of local models {
 local ++j
 matrix tmp = A[`i', 3*`j'-2]
 if tmp[1,1]<. {
 matrix colnames tmp = `model'
 matrix b = nullmat(b), tmp
 matrix tmp[1,1] = A[`i', 3*`j'-1]
 matrix se = nullmat(se), tmp
 matrix tmp[1,1] = A[`i', 3*`j']
 matrix p = nullmat(p), tmp
 }
 }
 ereturn post b
 quietly estadd matrix se
 quietly estadd matrix p
 eststo `name'
 qui cou 
 qui estadd scalar obs=r(N)
 
 }

esttab, se mtitle noobs star(* .1 ** .05 *** .01) stats(obs)

matrix drop A


* Non-Single
cap: drop RELINC1 RELPX1
gen RELINC1=post_relinc
gen RELPX1 = post_relpx

probit baseline_finalrank RELINC1 RELPX1 $controls if single==0 , robust
eststo row1

ivprobit baseline_finalrank (RELINC1 RELPX1 = relinc_diff relpx_diff) relpx_cfact relinc_cfact $controls if single==0 , vce(robust)
eststo row2

ivprobit baseline_finalrank (RELINC1 RELPX1 = relinc_diff relpx_diff) relpx_cfact relinc_cfact $controls $perceptions if single==0, vce(robust)
eststo row3


esttab, keep(RELINC1 RELPX1) order(RELINC1 RELPX1) se

mat list r(coefs)
mat define A=r(coefs)
eststo drop row*
local rnames : rownames A
local models : coleq A
local models : list uniq models

local i=0
foreach name in RELINC1 RELPX1 {
 local ++i
 local j 0
 cap: matrix drop b
 cap: matrix drop se
 cap: matrix drop p
 foreach model of local models {
 local ++j
 matrix tmp = A[`i', 3*`j'-2]
 if tmp[1,1]<. {
 matrix colnames tmp = `model'
 matrix b = nullmat(b), tmp
 matrix tmp[1,1] = A[`i', 3*`j'-1]
 matrix se = nullmat(se), tmp
 matrix tmp[1,1] = A[`i', 3*`j']
 matrix p = nullmat(p), tmp
 }
 }
 ereturn post b
 quietly estadd matrix se
 quietly estadd matrix p
 eststo `name'
 qui cou if single==0 
 qui estadd scalar obs=r(N)
 
 }

esttab, se mtitle noobs star(* .1 ** .05 *** .01)

matrix drop A


* Single
cap: drop RELINC2 RELPX2
gen RELINC2=post_relinc
gen RELPX2= post_relpx

probit baseline_finalrank RELINC2 RELPX2 $controls if single==1, robust
eststo row1

ivprobit baseline_finalrank (RELINC2 RELPX2 = relinc_diff relpx_diff) relpx_cfact relinc_cfact $controls if single==1, vce(robust)
eststo row2

ivprobit baseline_finalrank (RELINC2 RELPX2 = relinc_diff relpx_diff) relpx_cfact relinc_cfact $controls $perceptions if single==1, vce(robust)
eststo row3

esttab, keep(RELINC2 RELPX2) order(RELINC2 RELPX2) se

mat list r(coefs)
mat define A=r(coefs)
eststo drop row*
local rnames : rownames A
local models : coleq A
local models : list uniq models

local i=0
foreach name in RELINC2 RELPX2 {
 local ++i
 local j 0
 cap: matrix drop b
 cap: matrix drop se
 cap: matrix drop p
 foreach model of local models {
 local ++j
 matrix tmp = A[`i', 3*`j'-2]
 if tmp[1,1]<. {
 matrix colnames tmp = `model'
 matrix b = nullmat(b), tmp
 matrix tmp[1,1] = A[`i', 3*`j'-1]
 matrix se = nullmat(se), tmp
 matrix tmp[1,1] = A[`i', 3*`j']
 matrix p = nullmat(p), tmp
 }
 }
 ereturn post b
 quietly estadd matrix se
 quietly estadd matrix p
 eststo `name'
 qui cou if single==1 
 qui estadd scalar obs=r(N)
 }

esttab, se mtitle noobs star(* .1 ** .05 *** .01)

matrix drop A

local t0 " & \multicolumn{3}{c}{Panel A: \$ \beta^{ER} \$ } & \multicolumn{3}{c}{Panel B: \$ \beta^{COL} \$} \\ \cmidrule(lr){2-4} \cmidrule(lr){5-7} "
local t1 " & Non-Single & Single  & All & Non-Single & Single & All \\"
local t2 " &(1)&(2)&(3)&(4)&(5)&(6) \\ \midrule"

esttab  RELINC1 RELINC2 RELINC0  RELPX1 RELPX2 RELPX0 using "results/AppendixTableB3.tex", se nonotes noconstant label replace booktabs gap compress  b(%9.3f) se(%9.3f) star(* 0.1 ** 0.05 *** 0.01) nonumbers mlabels(none) eqlabels(none) coeflabels(row1 "Observational" row2 "Experimental" row3 "Experimental, Additional Controls") posthead("`t0'" "`t1'" "`t2'")

